package com.cyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyz.dao.ClassesDao;
import com.cyz.dao.StudentClassDao;
import com.cyz.dao.StudentDao;
import com.cyz.entity.Classes;
import com.cyz.entity.Student;
import com.cyz.entity.StudentClass;
import com.cyz.vo.StuClassesDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ClassesDao classesDao;

    @Autowired
    private StudentClassDao studentClassDao;

    @PostMapping("/sign")
    public String sign(String name, String account, String pwd){
        Student entity = studentDao.selectOne(new QueryWrapper<Student>().eq("account", account));
        if (entity != null){
            return "该用户已经存在";
        }
        Student newUser = new Student();
        newUser.setName(name);
        newUser.setAccount(account);
        newUser.setPwd(pwd);
        int insert = studentDao.insert(newUser);
        return insert == 0 ? "添加失败" : "添加成功";
    }

    @PostMapping("/login")
    public String login(String account, String pwd){
        Student entity = studentDao.selectOne(new QueryWrapper<Student>().eq("account", account)
                .eq("pwd", pwd));
        return entity != null ? "登录成功" : "登录失败";
    }

    @PostMapping("/student")
    public List<Student> getStudent(){
        return studentDao.selectList(new QueryWrapper<>());
    }

    @PostMapping("/classes")
    public List<Classes> getClasses(){
        return classesDao.selectList(new QueryWrapper<>());
    }

    @PostMapping("/info")
    public List<StuClassesDto> getInfo(){
        List<StudentClass> studentClasses = studentClassDao.selectList(new QueryWrapper<>());
        List<StuClassesDto> collect = studentClasses.stream().map(item -> {
            StuClassesDto stuClassesDto = new StuClassesDto();
            Student student = studentDao.selectById(item.getStuId());
            Classes classes = classesDao.selectById(item.getClassId());
            stuClassesDto.setStuId(student.getId());
            stuClassesDto.setStuName(student.getName());
            stuClassesDto.setClsId(classes.getId());
            stuClassesDto.setClsName(classes.getName());
            return stuClassesDto;
        }).collect(Collectors.toList());
        return collect;
    }

    @PostMapping("getUser/{account}")
    public Student getUser(@PathVariable String account){
        return studentDao.selectOne(new QueryWrapper<Student>().eq("account", account));
    }

    @PostMapping("getClassID")
    public List<Integer> getClassesId(){
        List<Classes> classes = classesDao.selectList(new QueryWrapper<>());
        return classes.stream().map(item -> item.getId()).collect(Collectors.toList());
    }

    @PostMapping("/getClsById/{id}")
    public Classes getDocById(@PathVariable int id){
        return classesDao.selectById(id);
    }

    @PostMapping("choose")
    public String choose(StuClassesDto stuClassesDto){
        StudentClass studentClass = studentClassDao.selectOne(new QueryWrapper<StudentClass>().eq("student_id", stuClassesDto.getStuId()).eq("classes_id", stuClassesDto.getClsId()));
        if (studentClass != null){
            return "已经选课";
        }
        StudentClass bean = new StudentClass();
        bean.setStuId(stuClassesDto.getStuId());
        bean.setClassId(stuClassesDto.getClsId());
        int insert = studentClassDao.insert(bean);
        return insert == 0 ? "失败" : "选课成功";
    }
}